//
//  WWTopViewCell.m
//  Shopping
//
//  Created by 张湾湾 on 16/8/20.
//  Copyright © 2016年 张湾湾. All rights reserved.
//

#import "WWTopViewCell.h"
#import "WWButton.h"

@interface WWTopViewCell()
/**订单*/
@property (nonatomic,weak)WWButton *orderButton;
/**券*/
@property (nonatomic,weak)WWButton *ticketButton;
/**消息*/
@property (nonatomic,weak)WWButton *messageButton;

@end
@implementation WWTopViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    WWButton * order = [[WWButton alloc]init];
    self.orderButton = order;
    [self addChildButton:order WithImage:@"order" andTitle:@"我的订单"andType:BtnTypeOrder];
    WWButton * ticket = [[WWButton alloc]init];
    self.ticketButton = ticket;
    [self addChildButton:ticket WithImage:@"submit" andTitle:@"优惠券" andType:BtnTypeTicket];
    WWButton * message = [[WWButton alloc]init];
    self.messageButton = message;
    [self addChildButton:message WithImage:@"message" andTitle:@"消息" andType:BtnTypeMessage];
}

- (void)addChildButton:(WWButton *)button WithImage:(NSString*)image andTitle:(NSString*)string andType:(BtnType)type{
    
    [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"v2_my_%@_icon",image]] forState:UIControlStateNormal];
    [button setTitle:string forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(didClickBut:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = type;
    [self.contentView addSubview:button];
}

#pragma mark - 点击事件 -
- (void)didClickBut:(WWButton*)sender{
    switch (sender.tag) {
        case BtnTypeOrder:
            NSLog(@"我的订单");
            [[NSNotificationCenter defaultCenter]postNotificationName:@"order" object:nil];
            break;
        case BtnTypeTicket:
            NSLog(@"优惠券");
            break;
        case BtnTypeMessage:
            NSLog(@"信息");
            break;
        default:
            break;
    }
     
}
- (void)layoutSubviews{
    [super layoutSubviews];
    [self.orderButton makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.contentView);
        make.top.bottom.mas_equalTo(self.contentView);
        make.trailing.mas_equalTo(self.ticketButton.mas_leading);
    }];
    [self.ticketButton makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(self.contentView);
        make.leading.mas_equalTo(self.orderButton.mas_trailing);
        make.trailing.mas_equalTo(self.messageButton.mas_leading);
        make.width.equalTo(self.orderButton.width);
    }];
    
    [self.messageButton makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(self.contentView);
        make.leading.mas_equalTo(self.ticketButton.mas_trailing);
        make.trailing.mas_equalTo(self.contentView);
        make.width.equalTo(self.ticketButton.width);
    }];
}
@end
